from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_del_script_group_dialog import Ui_Dialog_del_script_group
from database.db import Database
from app.window.msg_box import Msg_box

class Del_script_group_signal(QObject):
    save = Signal(str)

    delete_script_group = Signal(str)

class Del_script_group_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Del_script_group_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_del_script_group()
        self.ui.setupUi(self)

        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Del_script_group_signal()
        self.msg = Msg_box(self)
        self.ui.pushButton.clicked.connect(self.slot_ok)

    def slot_show(self):
        self.ui.comboBox.clear()
        # 加载分组数据
        db = Database()
        group_list = db.select('script_group',['name'])
        for data in group_list:
            self.ui.comboBox.addItem(data[0])
        
        self.show()

    def slot_ok(self):
        group_name = self.ui.comboBox.currentText()
        if not self.msg.msg_is_ok_cancel(f'确定删除分组 {group_name} ？\n删除后,该组内的所有脚本都将删除'):
            return

        db = Database()
        # 删除组
        db.delete('script_group',f'name="{group_name}"')

        #删除脚本
        db.delete('scripts',f'group_name="{group_name}"')
        
        db.close_conn()

        # 刷新combobox
        index = self.ui.comboBox.currentIndex()
        self.ui.comboBox.removeItem(index)

        # 通知public_script_widget 刷新数据
        self.signal.delete_script_group.emit(group_name)
            

